package com.darly.rnmodule.component.store;

import android.content.Context;
import android.graphics.Point;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.darly.rnmodule.R;
import com.darly.rnmodule.component.BaseComponent;
import com.darly.rnmodule.obs.RNModule;
import com.facebook.react.ReactInstanceManager;
import com.facebook.react.ReactRootView;

/**
 * Description TODO: 商城组件
 * Package com.darly.rnmodule.component.store
 * Author fronchdarly
 * Date 2021/5/26 2:33 PM
 * Where: 陕西西安
 * How: 特别一般的公司
 */
public class StoreComponent extends BaseComponent {

    private ReactRootView rootView;
    public static final String COMMODITY_TITLE_COMMON_NAME = "StoreDialog";

    public StoreComponent(Context context) {
        super(context);
    }

    public StoreComponent(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public StoreComponent(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected View getComponentView() {
        if (rootView == null) {
            rootView = new ReactRootView(getContext());
            rootView.setLayoutParams(new FrameLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT
            ));
            rootView.setBackgroundColor(getResources().getColor(R.color.cart_back));
            rootView.startReactApplication(getInstanceManager(), getCommonName(), getLaunchOptions());
        }
        return rootView;
    }

    @Override
    public ReactInstanceManager getInstanceManager() {
        return  RNModule.mReactNativeHost.getReactInstanceManager();
    }

    @Override
    public String getCommonName() {
        return COMMODITY_TITLE_COMMON_NAME;
    }

    @Override
    public Bundle getLaunchOptions() {
        Bundle bundle = new Bundle();
        return bundle;
    }

    @Override
    public void unMount() {
        if (rootView!=null){
            rootView.unmountReactApplication();
        }
    }
}
